/**
 * @(#) popAnnuitySavingMediator.as
 *
 * @date		2009. 04. 20.
 * @version		1.0
 * @author		Jo, ChangHun
 * @description	연말정산 메인 view - Mediator
 * 				연말정산 - 탭6 의료비 공제 팝업창 Logic을 관리 
 */
 package com.skt.common_hr.yearadjustment.view
 {
 	import com.skt.common_hr.common.PsHrAlert;
 	import com.skt.common_hr.common.Variables;
 	import com.skt.common_hr.common.puremvc.CommonMediator;
 	import com.skt.common_hr.yearadjustment.YearAdjustmentFacade;
 	import com.skt.common_hr.yearadjustment.model.AnnuitySavingProxy;
 	import com.skt.common_hr.yearadjustment.model.BankListProxy;
 	import com.skt.common_hr.yearadjustment.model.vo.AnnuitySavingVO;
 	import com.skt.common_hr.yearadjustment.view.components.popAnnuitySaving;
 	import com.skt.common_hr.yearadjustment.view.components.popAnnuitySaving_fnu;
 	import com.skt.common_hr.yearadjustment.view.components.popMedicalSubtraction;
 	import com.wcl.utils.DateUtil;
 	
 	import flash.events.Event;
 	
 	import mx.collections.ArrayCollection;
 	import mx.managers.PopUpManager;
 	
 	import org.puremvc.as3.multicore.interfaces.INotification;
 	
 	
 	public class popAnnuitySavingMediator extends CommonMediator
 	{
 		public static const NAME:String = "popAnnuitySavingMediator";
 		public var v_year:String = "";
		[Bindable] public var v_enable:Boolean = true;
//shgoo 2010.01.12
		//추가, 수정 구분
		private var bAdd:Boolean = true;
				 		
 		/**
 		 * 생성자
 		 * 
 		 * @param
 		 * @return void
 		 * */
 		public function popAnnuitySavingMediator( viewComponent:Object ):void
 		{
 			super(NAME, viewComponent);
 			view.callLater(initView);
 		}
 		
 		/**
 		 * View를 초기화 한다.
 		 * 	- View에 기본사항을 출력하고 data를 load
 		 * @param
 		 * @return void
 		 * */
 		public function initView():void
 		{
 			view.addEventListener(popMedicalSubtraction.OkInput, onClickSave);
 			view.addEventListener(popMedicalSubtraction.CancelInput, onCancelInput);

 			view._isEnable = v_enable;
 			loadAnnuityTypeList();
 			loadBankList();
 		}
 		
//shgoo 2010.01.11 		
 		//추가버튼 클릭 이벤트
 		private function onAdd(event:Event):void {
			bAdd = true;
			/*
 			if(view.RADIO.selectedValue == "X") {	//국세청 자료 총합으로 입력 인경우
				//항목 추가
				view.addArray();
 			}
 			else {	//국세청 자료 총합 입력이 아닌경우
 				//사업자  번호 유효성 체크
 				var proxy:BizNumValidProxy = facade.retrieveProxy(BizNumValidProxy.NAME) as BizNumValidProxy;
 				proxy.load(view.D_COMCD.text);
 			}
 			*/
 		}
 		
 		//수정버튼 클릭 이벤트
 		private function onModify(event:Event):void {
			bAdd = false;
			/*
 			if(view.RADIO.selectedValue == "X") {	//국세청 자료 총합으로 입력 인경우
				//항목 수정
				view.onModifyItem();
 			}
 			else {	//국세청 자료 총합 입력이 아닌경우
 				//사업자  번호 유효성 체크
 				var proxy:BizNumValidProxy = facade.retrieveProxy(BizNumValidProxy.NAME) as BizNumValidProxy;
 				proxy.load(view.D_COMCD.text);
 			}
 			*/
 		}

///////////////////
 		
 		//연금보혐로 공제유형
 		private function loadAnnuityTypeList():void{
 			var proxy:AnnuitySavingProxy = facade.retrieveProxy(AnnuitySavingProxy.NAME) as AnnuitySavingProxy;
 			proxy.loadAnnuityType(v_year);
 		}
 		
 		private function loadBankList():void{
 			var proxy:BankListProxy= facade.retrieveProxy(BankListProxy.NAME) as BankListProxy;
 			proxy.loadBankList();
 		}
 		
 		
 		//기본 데이터 조회
 		private function loadAnnuitySavingList():void{
 			var proxy:AnnuitySavingProxy = facade.retrieveProxy(AnnuitySavingProxy.NAME) as AnnuitySavingProxy;
 			proxy.load(Variables.yearPernr, v_year);
 		}
 		
 		//저장
 		private function onClickSave(event:Event):void{
 			
 			var savingList:Array = new Array();
	 		for each(var rec:AnnuitySavingVO in view.annuityList)
	 		{
				var obj:Object = new Object();
				obj.PREIN = rec.PREIN;			//종(전)근무지
				obj.PNSTY = rec.PNSTY;			//비용의 2차 범주
				obj.PNSTT = rec.PNSTT;			
				obj.BETRG = rec.BETRG;			//금액
				obj.FINCO = rec.FINCO;			//금융기관코드
				obj.INSNM = rec.INSNM;			//연금저축 금융사명
				obj.ACCNO = rec.ACCNO;			//계좌번호
				obj.BEGDA = DateUtil.toBasicString(rec.BEGDA);			//시작일
				obj.ENDDA = DateUtil.toBasicString(rec.ENDDA);			//종료일
				
	 			savingList.push(obj);
	 		}
 			
 			var proxy:AnnuitySavingProxy = facade.retrieveProxy(AnnuitySavingProxy.NAME) as AnnuitySavingProxy;
 			proxy.save(Variables.yearPernr, v_year, savingList);
 		}
 		
 		//팝업 닫기
 		private function onCancelInput(event:Event):void
 		{
 			facade.removeMediator(popAnnuitySavingMediator.NAME);
 			PopUpManager.removePopUp(view);
 		}
 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					AnnuitySavingProxy.LOAD_ANNUITY_SAVING_COMPLETE,
					AnnuitySavingProxy.LOAD_ANNUITY_SAVING_FAILED,
					AnnuitySavingProxy.LOAD_ANNUITY_TYPE_LIST_COMPLETE,
					AnnuitySavingProxy.LOAD_ANNUITY_TYPE_LIST_FAILED,
					AnnuitySavingProxy.SAVE_ANNUITY_SAVING_COMPLETE,
					AnnuitySavingProxy.SAVE_ANNUITY_SAVING_FAILED,
					BankListProxy.LOAD_BANK_LIST_COMPLETE,
					BankListProxy.LOAD_BANK_LIST_FAILED
					];
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName()) 
			{
				case AnnuitySavingProxy.LOAD_ANNUITY_SAVING_COMPLETE:
					view.annuityList = (note.getBody() as AnnuitySavingProxy).annuityData;
					break;
				case AnnuitySavingProxy.LOAD_ANNUITY_SAVING_FAILED:
					PsHrAlert.showMessage((note.getBody() as AnnuitySavingProxy).msgStr);
					break;
				case AnnuitySavingProxy.SAVE_ANNUITY_SAVING_COMPLETE:
					onCancelInput(null);
					facade.sendNotification(YearAdjustmentFacade.RELOAD_BASIC_DATA);
					//facade.sendNotification(YearAdjustmentView.CHECK_REQ_MEDICALSUBTRACTION);
					break;
				case AnnuitySavingProxy.SAVE_ANNUITY_SAVING_FAILED:
					PsHrAlert.showMessage((note.getBody() as AnnuitySavingProxy).msgStr);
					break;
				case AnnuitySavingProxy.LOAD_ANNUITY_TYPE_LIST_COMPLETE:
					view.annuityTypeList = (note.getBody() as AnnuitySavingProxy).annuityTypeList;
					loadAnnuitySavingList();
					break;
				case AnnuitySavingProxy.LOAD_ANNUITY_TYPE_LIST_COMPLETE:
					PsHrAlert.showMessage((note.getBody() as AnnuitySavingProxy).msgStr);
					break;
				case BankListProxy.LOAD_BANK_LIST_COMPLETE:
					view.bankList = (note.getBody() as BankListProxy).bankList;
					break;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
//shgoo 2010.01.07		
		//대상자 주민번호 추출
		private function getValidInhabitants(arrSrc:ArrayCollection):ArrayCollection {
			var arrDest:ArrayCollection = new ArrayCollection();
			
			//주민등록번호 선택 텍 스트 			
			arrDest.addItem(arrSrc.getItemAt(0));
			
			for each ( var rec:Object in arrSrc) {
				if(rec.DPTID == "X") {
					arrDest.addItem(rec);
				}
			}
			
			return arrDest;
		}
///////////////////
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
/* 		private function get view():popAnnuitySaving
		{
			return viewComponent as popAnnuitySaving;
		} */
		
		private function get view():*
		{
			return viewComponent as popAnnuitySaving;
//			
//			if(Variables.company == 'FNU') {
//				return viewComponent as popAnnuitySaving_fnu;	
//			}
//			else {
//				return viewComponent as popAnnuitySaving;
//			}
//			
		}
	}
 }